﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
using GreenyMeter.DOM.DataBaseAcces;
using GreenyMeter.DOM.Tools;
using GreenyMeter.DOM.Enums;


namespace GreenyMeter.DOM.Objets
{
    public class Users
    {


        #region Atributes

        private const string ConnectionName = "MusikWordCS";

        private int _userID;
        public int Id
        {
            get { return _userID; }
            set { _userID = value; }
        }
        public string Name
        {
            get { return String.Format("{0} {1}", _firstName.Trim(), _lastName.Trim()); }
        }

        private string _firstName;

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
        private string _lastName;

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        private int _cityID;
        public int CityID
        {
            get { return _cityID; }
            set { _cityID = value; }
        }

        private string _email;
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        private string _password;
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private bool _gender;
        public bool Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        private int _yob;
        public int YOB
        {
            get { return _yob; }
            set { _yob = value; }
        }
        public Users()
        {
            _userID = -1;
        }

        private Users(DataRow row)
        {
            if (!row.IsNull("UserID"))
            {
                _userID = int.Parse(row["UserID"].ToString());
            }

            if (!row.IsNull("FirstName"))
            {
                _firstName = row["FirstName"].ToString();
            }

            if (!row.IsNull("LastName"))
            {
                _lastName = row["LastName"].ToString();
            }

            if (!row.IsNull("UserName"))
            {
                _userName = row["UserName"].ToString();
            }

            if (!row.IsNull("CityID"))
            {
                _cityID = (int)row["CityID"];
            }

            if (!row.IsNull("Email"))
            {
                _email = row["Email"].ToString();
            }

            if (!row.IsNull("Password"))
            {
                _password = row["Password"].ToString();
            }

            if (!row.IsNull("Gender"))
            {
                _gender = (bool)row["Gender"];
            }

            if (!row.IsNull("YOB"))
            {
                _yob = (int)row["YOB"];
            }

        }
        #endregion

        #region Methods

        public static Users getByEmail(string Email)
        {

            if (Email != null)
            {
                SqlDataAcces sqldataacces = new SqlDataAcces();
                List<SqlDaAccesParameters> sqlparams = new List<SqlDaAccesParameters>();
                sqlparams.Add(new SqlDaAccesParameters("@Email", Email));
                DataTable dt = sqldataacces.GetDataTable("usp_UserSelectbyId", sqlparams, ConnectionName);
                foreach (DataRow row in dt.Rows)
                {
                    return new Users(row);
                }
                return null;
            }
            else {
                return null;
            }

        }

        public static Users getByUserName(string UserName)
        {
            SqlDataAcces sqldataacces = new SqlDataAcces();
            List<SqlDaAccesParameters> sqlparams = new List<SqlDaAccesParameters>();
            sqlparams.Add(new SqlDaAccesParameters("@UserName", UserName));
            DataTable dt = sqldataacces.GetDataTable("usp_UserSelectbyId", sqlparams, ConnectionName);
            foreach (DataRow row in dt.Rows)
            {
                return new Users(row);
            }
            return null;
        }

        public static Users Login(string email, string password)
        {

            Utility objpasword = new Utility();
            password = objpasword.CalculateMD5Hash(password);

            SqlDataAcces sqldataacces = new SqlDataAcces();
            List<SqlDaAccesParameters> sqlparams = new List<SqlDaAccesParameters>();
            sqlparams.Add(new SqlDaAccesParameters("@Email", email));
            sqlparams.Add(new SqlDaAccesParameters("@Password", password));
            DataTable dt = sqldataacces.GetDataTable("usp_UserSelectbyId", sqlparams, ConnectionName);
            foreach (DataRow row in dt.Rows)
            {
                return new Users(row);
            }
            return null;
        }

        public static Users getById(string UserID)
        {
            SqlDataAcces sqldataacces = new SqlDataAcces();
            List<SqlDaAccesParameters> sqlparams = new List<SqlDaAccesParameters>();
            sqlparams.Add(new SqlDaAccesParameters("@UserID", UserID));
            DataTable dt = sqldataacces.GetDataTable("usp_UserSelectbyId", sqlparams, ConnectionName);
            foreach (DataRow row in dt.Rows)
            {
                return new Users(row);
            }
            return null;
        }


        public static Users create(string FirstName, string LastName, string UserName, int cityID, string email, string Password, bool Gender, int YOB, bool AcceptEmailNotifications, out CreateUserStatus UserStatus)
        {

            Utility validatemail = new Utility();
            UserStatus = CreateUserStatus.Success;

            if (getByEmail(email) != null) { UserStatus = CreateUserStatus.DuplicateEmail; }
            if (getByUserName(UserName) != null) { UserStatus = CreateUserStatus.DuplicateUserName; }
            if (validatemail.isEmail(email) == false) { UserStatus = CreateUserStatus.InvalidEmail; }
            //if (validatemail.isRealDomain(email) == false) { UserStatus = CreateUserStatus.InvalidEmail; }

            
            if (UserStatus == CreateUserStatus.Success)
            {

                SqlDataAcces sqlDataAcces = new SqlDataAcces();
                List<SqlDaAccesParameters> sqlParams = new List<SqlDaAccesParameters>();

                Utility objpasword = new Utility();
                string md5Pasword = objpasword.CalculateMD5Hash(Password);

                sqlParams.Add(new SqlDaAccesParameters("@FirstName", FirstName));
                sqlParams.Add(new SqlDaAccesParameters("@LastName", LastName));
                sqlParams.Add(new SqlDaAccesParameters("@UserName", UserName));
                sqlParams.Add(new SqlDaAccesParameters("@cityID", cityID));
                sqlParams.Add(new SqlDaAccesParameters("@email", email));
                sqlParams.Add(new SqlDaAccesParameters("@Password", md5Pasword));
                sqlParams.Add(new SqlDaAccesParameters("@Gender", Gender));
                sqlParams.Add(new SqlDaAccesParameters("@YOB", YOB));
                sqlParams.Add(new SqlDaAccesParameters("@ClearPassword", Password));
                sqlParams.Add(new SqlDaAccesParameters("@UserStatus", 1));
                sqlParams.Add(new SqlDaAccesParameters("@AcceptEmailNotifications", AcceptEmailNotifications));

                DataTable dt = sqlDataAcces.GetDataTable("usp_UserInsert", sqlParams, ConnectionName);
                foreach (DataRow row in dt.Rows)
                {
                    return new Users(row);
                }
                return null;
            }
            return null;


        }

        //public static Users ValidateCustomer(string userName, string userPassword)
        //{
        //    SqlDataAcces sqlDataAcces = new SqlDataAcces();
        //    List<SqlDaAccesParameters> sqlParams = new List<SqlDaAccesParameters>();
        //    sqlParams.Add(new SqlDaAccesParameters("@Email", userName));
        //    sqlParams.Add(new SqlDaAccesParameters("@Password", userPassword));
        //    DataTable dt = sqlDataAcces.GetDataTable("usp_CustomersAuthenticate", sqlParams, ConnectionName);
        //    foreach (DataRow row in dt.Rows)
        //    {
        //        return new Users(row);
        //    }
        //    return null;
        //}

        #endregion
    }
}